﻿using System;
using System.Xml.Linq;

namespace YArchitech.Temp
{
	public class HYDuctSpecification : IHYSpecification, IComparable, IHYXmlSerial
	{
		public object Tag { get; set; }

		public HYShapeProfile Shape
		{
			get
			{
				return this.m_Profile;
			}
			set
			{
				this.m_Profile = value;
			}
		}

		public string ShapeString
		{
			get
			{
				return HYShapeDescription.ToString(this.m_Profile);
			}
		}

		public int Diameter
		{
			get
			{
				return this.a;
			}
			set
			{
				this.a = value;
			}
		}

		public int Width
		{
			get
			{
				return this.a;
			}
			set
			{
				this.a = value;
			}
		}

		public int Height
		{
			get
			{
				if (this.Shape == HYShapeProfile.Round)
				{
					return this.a;
				}
				return this.b;
			}
			set
			{
				this.b = value;
			}
		}

		public double Factor
		{
			get
			{
				return 1.0 * (double)this.Width / (double)this.Height;
			}
		}

		public double Area
		{
			get
			{
				double num = 0.0;
				switch (this.m_Profile)
				{
				case HYShapeProfile.Round:
					num = GeUtils.Area_Round((double)this.a);
					break;
				case HYShapeProfile.Rectangular:
					num = GeUtils.Area_Rectangular((double)this.a, (double)this.b);
					break;
				case HYShapeProfile.Oval:
					num = GeUtils.Area_Oval((double)this.a, (double)this.b);
					break;
				}
				return num / 1000.0 / 1000.0;
			}
		}

		public double Circumference
		{
			get
			{
				double num = 10.0;
				switch (this.m_Profile)
				{
				case HYShapeProfile.Round:
					num = GeUtils.Circumference_Round((double)this.a);
					break;
				case HYShapeProfile.Rectangular:
					num = GeUtils.Circumference_Rectangular((double)this.a, (double)this.b);
					break;
				case HYShapeProfile.Oval:
					num = GeUtils.Circumference_Oval((double)this.a, (double)this.b);
					break;
				}
				return num / 1000.0;
			}
		}

		public double Rs
		{
			get
			{
				return Hydrokinetics.Rs(this.Area, this.Circumference);
			}
		}

		public double D
		{
			get
			{
				return Hydrokinetics.D(this.Rs);
			}
		}

		public bool IsValid
		{
			get
			{
				return this.Width > 0 && this.Height > 0;
			}
		}

		public HYDuctSpecification(HYShapeProfile shapeProfile = HYShapeProfile.Rectangular, int DiaOrWidth = 800, int h = 320)
		{
			this.Shape = shapeProfile;
			this.a = DiaOrWidth;
			this.b = h;
		}

		private int GetValue()
		{
			return 10000 * this.Width + this.Height;
		}

		public int CompareTo(object obj)
		{
			if (!(obj is HYDuctSpecification))
			{
				return 1;
			}
			HYDuctSpecification hyductSpecification = obj as HYDuctSpecification;
			if (this.GetValue() == hyductSpecification.GetValue())
			{
				return 0;
			}
			if (this.GetValue() >= hyductSpecification.GetValue())
			{
				return 1;
			}
			return -1;
		}

		public static bool operator <(HYDuctSpecification left, HYDuctSpecification right)
		{
			return left.GetValue() < right.GetValue();
		}

		public static bool operator >(HYDuctSpecification left, HYDuctSpecification right)
		{
			return left.GetValue() > right.GetValue();
		}

		public double GetVelocity(double flow)
		{
			HYShapeProfile shape = this.Shape;
			if (shape == HYShapeProfile.Round)
			{
				return Hydrokinetics.Velocity_Round(flow, (double)this.Diameter / 1000.0);
			}
			if (shape == HYShapeProfile.Rectangular)
			{
				return Hydrokinetics.Velocity_Rectangular(flow, (double)this.Width / 1000.0, (double)this.Height / 1000.0);
			}
			return 0.0;
		}

		public void WriteXml(XElement xe)
		{
			xe.SetAttributeValue("Shape", (int)this.Shape);
			xe.SetAttributeValue("Width", this.a);
			xe.SetAttributeValue("Height", this.Height);
		}

		public void ReadXml(XElement xe)
		{
			int profile = (int)xe.Attribute("Shape");
			this.m_Profile = (HYShapeProfile)profile;
			this.a = (int)xe.Attribute("Width");
			this.b = (int)xe.Attribute("Height");
		}

		private HYShapeProfile m_Profile;

		private int a;

		private int b;
	}
}
